#import "CameraGalleryManager.h"
#import "NetWorkManager.h"
#import "PopUIManager.h"
#import "UserInfo.h"
#import "JsonUtil.h"
#import "ImageUtil.h"

static CameraGalleryManager* cgMgr;
static UIViewController* super_view_controller;

@implementation CameraGalleryManager

+(CameraGalleryManager*) getInstanse{
    if (cgMgr == nil) {
        cgMgr = [[CameraGalleryManager alloc]init];
    }
    return cgMgr;
}

-(void)takePhoto:(UIViewController*) super_vc finishBlock:(FinishBlock)block{
    self.finishBlockCallback = block;
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    super_view_controller = super_vc;
    if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        [super_vc presentViewController:picker animated:YES completion:nil];
    }else{
        NSLog(@"模拟器中无法打开Camera!");
    }
}

-(void)gallerySelect:(UIViewController*) super_vc finishBlock:(FinishBlock)block{
    self.finishBlockCallback = block;
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    super_view_controller = super_vc;
    if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        [super_vc presentViewController:picker animated:YES completion:nil];
    }
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    NSLog(@"图片选择完成.");
    if ([type isEqualToString:@"public.image"]) {
        NSLog(@"%@",info);
        
        UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];  //获取裁剪后的图片
        image = [[ImageUtil getInstance] createNewScaleImage:image size:CGSizeMake(200, 200)];
        [picker dismissViewControllerAnimated:YES completion:nil];
        self.finishBlockCallback(image, @"");
        /*
        //保存图片到本地
//        [self saveImageWithName:@"image.jpg" data:data];
        
        [picker dismissViewControllerAnimated:YES completion:nil];
        //显示等待进度条
        PopUIManager *popUIMgr = [PopUIManager getInstanse];
        [popUIMgr showWaitingUI:super_view_controller.view];
        [[NetWorkManager getInstanse] uploadImageToServer:image FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                NSDictionary *dict = [JsonUtil convertDataToDictionary:data];
                NSLog(@"upload Image Asyn result: %@", dict);
            if ([[dict objectForKey:@"retCode"]integerValue] == 1) {
                //图片上传成功
                UIImage* temp = [[ImageUtil getInstance] circleImage:image withParam:0]; //裁剪圆形图像
                NSString* avatar = [dict objectForKey:@"avatar"];
                self.finishBlockCallback(temp, avatar); //设置用户图像
            }else{//图片上传失败
    
            }
            [popUIMgr removeWaitingUI];
        }];
         */
        
    }
}

-(void)saveImageWithName:(NSString *) imgName data:(NSData *)imgData
{
    NSString *documentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/"];
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    [fileMgr createDirectoryAtPath:documentsPath withIntermediateDirectories:YES attributes:nil error:nil];
    [fileMgr createFileAtPath:[documentsPath stringByAppendingString:imgName] contents:imgData attributes:nil];
    NSLog(@"save path: %@",[documentsPath stringByAppendingString:imgName]);
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    NSLog(@"你取消了图片选择");
    [picker dismissViewControllerAnimated:YES completion:nil];
}




@end
